package com.recyclerviewdemo.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.recyclerviewdemo.R;
import com.recyclerviewdemo.entity.GankResult;
import com.recyclerviewdemo.entity.MeiZi;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yang on 2016/9/2.
 */
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder>{


    private Context context;
    private List<GankResult.ResultsBean> picData = new ArrayList<>();
    private List<GankResult.ResultsBean> textData = new ArrayList<>();
//    private OnItemClickListener mOnItemClickListener;
//    private OnItemLongClickListener mOnItemLongClickListener;


    //构造器，接受数据集
    public RecyclerviewAdapter(Context context){
        this.context = context;

    }

    public List<GankResult.ResultsBean> getTextData() {
        return textData;
    }

    public void setTextData(List<GankResult.ResultsBean> textData) {
        this.textData = textData;
    }

    public List<GankResult.ResultsBean> getPicData() {
        return picData;
    }

    public void setPicData(List<GankResult.ResultsBean> picData) {
        this.picData = picData;
    }

//    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
//        this.mOnItemClickListener = mOnItemClickListener;
//    }
//
//    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
//        this.mOnItemLongClickListener = mOnItemLongClickListener;
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,parent,false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.setDataAndRefreshUI(picData.get(position), textData.get(position));
        //判断是否设置了监听器
//        if(mOnItemClickListener != null){
//            //为ItemView设置监听器
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    int position = holder.getLayoutPosition(); // 1
//                    mOnItemClickListener.onItemClick(holder.itemView,position); // 2
//                }
//            });
//        }
//        if(mOnItemLongClickListener != null){
//            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    int position = holder.getLayoutPosition();
//                    mOnItemLongClickListener.onItemLongClick(holder.itemView,position);
//                    //返回true 表示消耗了事件 事件不会继续传递
//                    return true;
//                }
//            });
//        }
    }


    @Override
    public int getItemCount() {
        return picData.size();
    }


//    public interface OnItemClickListener{
//        void onItemClick(View view,int position);
//    }
//
//    public interface OnItemLongClickListener{
//        void onItemLongClick(View view,int position);
//    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView mTextView;
        private ImageView mImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.meizi);
            mTextView = (TextView) itemView.findViewById(R.id.Desc);
        }

        public void setDataAndRefreshUI(GankResult.ResultsBean picdata, GankResult.ResultsBean textdata) {
            try {
                mTextView.setText(textdata.getDesc());
                // 自动加载图片
                Picasso.with(context)
                        .load(picdata.getUrl())
                        .error(R.drawable.meizi)
                        .fit()
                        .centerCrop()
                        .into(mImageView);
            }catch (Exception e){
                Log.e("---->adapter", e.toString());
            }
        }
    }
}
